<?php

namespace App\Http\Controllers\Webdisk\IO;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use function App\Webdisk\getFileSize;
use function App\Webdisk\getSuffix;

class LocalFile extends Controller
{
    function getRemoteFile(Request $req){
        $uri = $req->attributes->get('uri');
        header('Content-Type: application/json');
        $res = [];
        foreach (NODE_LIST as $key => $value) {
            $addr = "{$value['addr']}/api/Resource/list";
            $curl_fp = curl_init($addr);
            curl_setopt($curl_fp,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($curl_fp,CURLOPT_POSTFIELDS,['path'=>"/$uri"]);
            $ret = json_decode( curl_exec($curl_fp),true );
            if($ret['status'] == 1){
                $res[] = [
                    'data'=>$ret['data'],
                    'link'=>$value['download'],
                    'name'=>$value['name']
                ];
            }
            curl_close($curl_fp);
        }
        return $res;
    }

    function download(Request $request){
        $uri = $request->attributes->get("uri");
        $filePath = env("RES_ROOT").'/'.urldecode("$uri");
        if(!file_exists($filePath)){
            return abort(404);
        }else{
            return response()->file($filePath);
        }
    }


    function getFileList(Request $request){
        // 获取基本属性
        $uri = $request->attributes->get("uri");
        $full_uri = preg_replace("/\/+$/",'',$_SERVER['REQUEST_URI']);
        $path = urldecode(env("RES_ROOT")."/$uri");
        $path = preg_replace('/\?.*$/','',$path);
        $res = [];
        if(isset($_GET['from'])){
            $path = preg_replace('/^\/pack|\.\./','',$_GET['from']);
            header("Location: /public/$path");
            return;
        }
        // 文件转到下载页面
        if(is_file($path)){
            $t = explode('/',$uri);
            $fileName = array_pop($t);
            return view('fileDownload',[
                'name'=> urldecode($fileName),
                'size'=> getFileSize($path),
                'uri'=>$uri,
                'type'=>getSuffix($fileName),
                'full_uri'=>$full_uri,
                'downloads'=>DOWNLOAD_NODE
            ]);
        }else{
        //  扫描目录，不存在就404
            try {
                $res = scandir($path);
            } catch (\Throwable $th) {
                return abort(404);
            }
        }

        // 分开文件与目录
        $dirs = [];$files = [];
        $uri = preg_replace('/\?.*$/','',$uri);
        $baseLink = env("PUBLIC_PREFIX")."$uri";
        foreach ($res as $key => $value) {
            if ( $value == '.' || $value == '..' ){
                continue;
            }
            $filePath = $path."/$value";
            $info = [
                'name'=>$value,
                'size'=>'-',
                'date'=>date("Y-m-d H:i",filemtime($filePath)),
                'link'=>"/$baseLink/". urlencode($value),
                'from'=>'origin'
            ];
            if(is_file($filePath)){
                $info['size'] = getFileSize($filePath);
                $info['type'] = getSuffix($filePath);
                $files[] = $info;
            }else{
                $dirs[] = $info;
            }
        }
        $data = [
            'files'=>$files,
            'dirs'=>$dirs,
            'uri'=>$uri == '' ? '/' : $uri,
            'full_uri'=>$full_uri,
            'path'=>$path
        ];
        return view("filelist",$data);
    }
}
